<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">
            <slot name="title"> 更改 </slot>
          </h5>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row">
              <label class="control-label col-3 text-end"> 服务费模板 </label>
              <div class="col-9">
                <select class="form-select" v-model.trim="code">
                  <template v-for="info in templates">
                    <option :value="info.templateCode">
                      {{ info.templateName }}
                    </option>
                  </template>
                </select>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { searchCustomerServiceTemplates } from "@/api/admin.js";
import $ from "jquery";

export default {
  props: {
    id: {
      type: String,
      default: "modalPrompt",
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      templates: [],
      code: "",

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({ code: this.code });
    },
    modal: function (code) {
      this.showModal();

      this.searchTemplates();
      // this.$nextTick(() => {
      //   console.log(this.username)
      //   this.username = username
      // })

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    searchTemplates: function () {
      // console.log('searchUsers')
      if (this.templates.length > 0) return;

      const params = {
        "sc.pageNo": 1,
        "sc.pageSize": 200,
      };

      searchCustomerServiceTemplates((v) => {
        // console.log(v)
        this.templates = v.dataList;
        // this.$nextTick(() => {
        //   this.username = username
        //   console.log(this.username)
        // })
      });
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl);
      }

      return this.myModal;
    },
  },
};
</script>
